//
//  ZBYTabBar.swift
//  新浪微博
//
//  Created by zby on 16/2/25.
//  Copyright © 2016年 zby. All rights reserved.
//

import UIKit

class ZBYTabBar: UITabBar {

    //重写init with frame方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //创建一个方法，用于创建视图和布局
    func setUpUI(){
        addSubview(composeButton)
    }
    
    
    //懒加载
    lazy var composeButton : UIButton = {
        //初始化一个button
        let button = UIButton(type: UIButtonType.Custom)
    
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        //设置按钮的图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Normal)
        
        //给按钮添加点击事件
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //按钮的点击事件
    func composeButtonClick(){
        print("我好帅")
    }
    
    //布局子控件
    override func layoutSubviews() {
        //设置角标
        var index: Int = 0
        
        let width: CGFloat = self.bounds.size.width/5
        let height: CGFloat = self.bounds.size.height
        
        //遍历系统TabBar的子控件
        for subview in subviews{
            //如果是UITabBarButton类型的话
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                //设置frame
                subview.frame = CGRectMake(CGFloat (index++) * width, 0, width, height)
                //如角标为2，则让其加1
                if index == 2 {
                    index++
                }
            }
        }
        
        //设置composeButton的frame
        composeButton.frame = CGRectMake(2 * width, 0, width, height)
    }
    
}












